layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;


    //监听单元格编辑
    table.on('edit(noticeTable)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        //编辑
        var suo = data.flag;
        if (suo == 1) {
            Feng.error("已报销,无法修改!");
            table.reload(Notice.tableId);
            return;
        }

        //return false;
        //var layerIndex = layer.load(3);
        $.ajax({
            type: "PUT",
            url: "/btyBxd/update",
            datatype: "json",
            data: data,
            success: function (data) {
                // if(data==2){
                //     Feng.error("已报销，不能修改");
                //     table.reload(Notice.tableId);
                // }
                //Feng.success("修改成功！");
                // table.reload(Notice.tableId);
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) {
                //layer.close(layerIndex);
                //50秒自动刷新一次
                //setTimeout(showTime,50000);
            }

        });
        //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
    });

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        trigger: 'click',
        // max: Feng.currentDate()
    });
    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#bh").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });
    // form.on('select(btyBxdaId)', function(data){
    //     Notice.search();
    // });
    form.on('select(flag)', function (data) {
        Notice.search();
    });
    form.on('select(status)', function (data) {
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)', function (obj) {
        $(obj.tr).siblings().css("background-color", "");
        obj.tr.css({"background-color": "#dadad9"});
    });
    /**
     * 检索摘要
     */
    var zyAjax = new $ax(Feng.ctxPath + "/btyBxd/zyList");
    var zyResult = zyAjax.start();
    $.each(zyResult, function( index, val ) {
        $("#zy").append("<option value='"+val.id+"'>"+val.name+"</option>");
    } );
    form.render('select');

    /**
     * 检索会计
     */
    var kjAjax = new $ax(Feng.ctxPath + "/btyBxd/getKj");
    var kjResult = kjAjax.start();
    $.each(kjResult, function (index, val) {
        $("#kj").append("<option value='" + val.kj + "'>" + val.kj + "</option>");
    });
    form.render('select');
    /**
     * 检索自采店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxd/zcDmList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#btyBxdaId").append("<option value='" + val.id + "'>" + val.dm + "</option>");
    });
    form.render('select');


    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {align: 'center', type: "numbers", title: '序号'},
            {field: 'status', align: "center",templet: '#statusTpl2', sort: true, title: '状态'},
            {field: 'bh', align: "center", sort: true, title: '报销单编号'},
            {field: 'dm', align: "center", sort: true, title: '店名', width: 150,},
            {field: 'ghs', align: "center", sort: false, title: '供货商', width: 90},
            {field: 'zcje', align: "center", sort: false, title: '支出金额', width: 90, totalRow: true},
            {field: 'bxr', align: "center", sort: false, title: '报销人', width: 80},
            {field: 'yhk', align: "center", sort: true, title: '银行卡号', width: 180},
            {field: 'zy', align: "center", sort: false, title: '摘要', width: 120},
            {field: 'bhyy', align: "center", sort: false, title: '打回原因', width: 100},
            {field: 'bz', align: "center", sort: false, title: '备注', width: 100},
            {field: 'kj', align: "center", sort: false, title: '会计', width: 80},
            {field: 'name', align: "center", sort: false, title: '操作员'},
            {field: 'createTime', align: "center", sort: true, title: '创建时间', width: 160,},
            // templet:'<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd") }}</div>'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 150}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['bh'] = $("#bh").val();
        queryData['dm'] = $("#dm").val();
        queryData['status'] = $("#status").val();
        queryData['flag'] = $("#flag").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['kj'] = $("#kj").val();
        queryData['btyBrandId'] = $("#btyBrandId").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.one = function () {
        var dm = $("#btyBxdaId").val();
        if (dm == "") {
            Feng.error("请选择店名!");
            return;
        }
        var zy = $("#zy").val();
        if (zy == "") {
            Feng.error("请选择摘要!");
            return;
        }
        var btyBxdaId = $("#btyBxdaId").val();
        var zcje = $("#zcje").val();
        var zy = $("#zy").val();
        var zyName=$("#zy").find("option:selected").text();
        var bz = $("#bz").val();
        //添加之前校验银行卡和行号和会计档案信息
        var daAjax = new $ax(Feng.ctxPath + "/btyBxda/info");
        daAjax.set("id",btyBxdaId);
        var daResult = daAjax.start();
        if(daResult.yhk===""|daResult.bz===""|daResult.kj=== ""){
            alert("请先在自采报销档案中维护银行卡,行号,会计的相关信息!");
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/btyBxd/addZc", function (data) {
            Feng.success("添加成功!");
            table.reload(Notice.tableId);
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set("btyBxdaId", btyBxdaId);
        ajax.set("zcje", zcje);
        ajax.set("zyid", zy);
        ajax.set("zy", zyName);
        ajax.set("bz", bz);
        ajax.start();
        // func.open({
        //     height: 1000,
        //     title: '添加报销单',
        //     content: Feng.ctxPath + '/btyBxd/btyBxd_add',
        //     tableId: Notice.tableId
        // });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyBxd/delete", function (data) {
                if (data == 2) {
                    Feng.error("已报销,不允许删除!");
                    return;
                }
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除报销单 ?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 920,
            title: '修改报销单',
            content: Feng.ctxPath + "/btyBxd/update_zc?id=" + data.id,
            tableId: Notice.tableId
        });
    };
    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        //获取导出Excel的数据
        var ajax = new $ax(Feng.ctxPath + "/btyBxd/listZcExport");
        var bh = $("#bh").val();
        var status = $("#status").val();
        var flag = $("#flag").val();
        var timeLimit = $("#timeLimit").val();
        var kj = $("#kj").val();
        var btyBrandId = $("#btyBrandId").val();
        ajax.set("bh", bh);
        ajax.set("status", status);
        ajax.set("flag", flag);
        ajax.set("timeLimit", timeLimit);
        ajax.set("kj", kj);
        ajax.set("btyBrandId", btyBrandId);
        var result = ajax.start();
        //身份证银行卡导出excel科学计数法问题
        var lists = result;
        for (index in lists) {
            var bh = lists[index].bh;
            var yhk = lists[index].yhk;
            lists[index].bh = bh + "\t";
            lists[index].yhk = yhk + "\t";

        }
        table.exportFile(tableResult.config.id, lists, 'xls');
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyBxd/listZc',
        page: true,
        height: "full-155",
        cellMinWidth: 100,
        limit: 20,
        totalRow: true,	//开启合计行
        cols: Notice.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 添加按钮点击事件
    $('#btnOne').click(function () {
        Notice.one();
    });
    // 审核按钮点击事件
    $('#btnOk').click(function () {
        Notice.confirmOk();
    });
    // 拒绝按钮点击事件
    $('#btnFalse').click(function () {
        Notice.confirmFalse();
    });
    // 导出excel
    $('#btnExp').click(function () {
        Notice.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
